from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
import util
import json

def index(request):
    theme = request.session['theme'] if 'theme' in request.session else 'mario'
    return render_to_response('index.html', {'theme': theme},
                              context_instance = RequestContext(request))

def user_register(request):
    if request.method == 'POST':
        user  = request.POST['user']
        pswd  = request.POST['pswd']
        pswd2 = request.POST['pswd2']
        email = request.POST['email']
        fname = request.POST['fname']
        lname = request.POST['lname']

        for i in [user, pswd, pswd2, email, fname, lname]:
            if len(i) < 1:
                user = None
                break

        if user is not None:
            if pswd == pswd2:
                try:
                    uobj = User.objects.create_user(username = user,
                                                    email = email,
                                                    password = pswd)
                    uobj.first_name = fname
                    uobj.last_name  = lname
                    uobj.save()

                    user = authenticate(username = user, password = pswd)
                    login(request, user)
                    return HttpResponse(json.dumps({'success': True}), mimetype = 'application/json')
                except Exception, e:
                    user = None
            else: user = None

        if user is None:
            return HttpResponse(json.dumps({'success': False}), mimetype = 'application/json')
    else:
        return render_to_response('register.html', context_instance = RequestContext(request));

def user_login(request):
    print 'logging in user'
    if request.method == 'POST':
        user = request.POST['user']
        pswd = request.POST['pswd']
        user = authenticate(username = user, password = pswd)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/')
            else: user = None

        if user is None :
            return HttpResponse(json.dumps({'success': True}), mimetype = 'application/json')
    else:
        return render_to_response('login.html', context_instance = RequestContext(request));

def user_logout(request):
    logout(request)
    return HttpResponseRedirect('/')

def store_session(request):
    args = dict(request.GET.items())
    for arg in args:
        request.session[arg] = args[arg]
    print request.session
    return HttpResponse(json.dumps({'success': True}), mimetype = 'application/json')

def loading(request):
    return render_to_response('loading.html')

def show_template(request, name):
    return render_to_response(name, context_instance = RequestContext(request))

def restart(request):
    util.kill()
    return render_to_response('sys_restart.html')

def update(request):
    updt = util.svn_up()
    util.kill()
    return render_to_response('sys_update.html', {
        'svn_out': updt[0],
        'svn_err': updt[1]
    })
